package com.sysu.ooad.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.sysu.ooad.dao.CommonDao;
import com.sysu.ooad.dao.StudentDao;
import com.sysu.ooad.entity.Course;
import com.sysu.ooad.entity.Group;
import com.sysu.ooad.entity.Student;

@Component("studentDao")
public class StudentDaoImpl extends CommonDao implements StudentDao{
	
	public void add(Student s) {
		hibernateTemplate.save(s);
	}
	
	@SuppressWarnings("unchecked")
	public Student findByName(String name) {
		List<Student> students = (List<Student>)hibernateTemplate.find("from Student s where s.name = ?", new Object[]{name});
		if(students.isEmpty()) return null;
		return students.get(0);
	}

	public void update(Student s) {
		hibernateTemplate.update(s);
	}

	@SuppressWarnings("unchecked")
	public List<Student> findByCourse(Course c) {
		List<Student> students = (List<Student>) hibernateTemplate.find("from Student s where s.course = ?", new Object[]{c});
		return students;
	}

	@SuppressWarnings("unchecked")
	public List<Student> findByGroup(Group g) {
		List<Student> students = (List<Student>) hibernateTemplate.find("from Student s where s.group = ?", new Object[]{g});
		return students;
	}

	@SuppressWarnings("unchecked")
	public List<Student> findWhoHaveNoGroup(Course c) {
		List<Student> students = (List<Student>) hibernateTemplate.find("from Student s where s.course = ? and s.group is null", new Object[]{c});
		return students;
	}

	public void merge(Student s) {
		hibernateTemplate.merge(s);
	}
}
